#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using PLACEHR.Web.Utilities;
using PLACEHR.Web.Categories.Management.Forms;

#endregion

namespace PLACEHR.Web.Categories.Management.Controls
{
    public partial class ApproveLeaveControl : UserControl
    {
        public ApproveLeaveControl()
        {
            InitializeComponent();
            InitialData();
            Retrieve();
        }

        private void InitialData()
        {
            lblEmployeeName.Text = LanLayer.GetTrans(122) + ":";
            lblLeaveType.Text = LanLayer.GetTrans(123) + ":";
            lblOperateTime.Text = LanLayer.GetTrans(133) + ":";
            lblOperator.Text = LanLayer.GetTrans(132) + ":";
            lblStatus.Text = LanLayer.GetTrans(131) + ":";

            colEmployeeName.Text = LanLayer.GetTrans(122);
            colEndTime.Text = LanLayer.GetTrans(130);
            colLeaveType.Text = LanLayer.GetTrans(123);
            colLength.Text = LanLayer.GetTrans(127);
            colOperateDate.Text = LanLayer.GetTrans(133);
            colOperator.Text = LanLayer.GetTrans(132);
            colStartTime.Text = LanLayer.GetTrans(129);
            colStatus.Text = LanLayer.GetTrans(131);
            colFinalBalance.Text = LanLayer.GetTrans(194);


            dtpOperateTime.Value = DateTime.Today.AddDays(-7);
            cbbVacationType.Items.Clear();
            List<VacationType_lkp> lstVacationType = Global.Context.VacationType_lkp.ToList();
            cbbVacationType.Items.Add("");
            foreach (var vacationType in lstVacationType)
            {
                ComboBoxItem item = new ComboBoxItem();
                item.Text = LanLayer.GetTrans(vacationType.Content_id.Value);
                item.Value = vacationType;

                cbbVacationType.Items.Add(item);
            }

            cbbStatus.Items.Add("");
            foreach (var status in Constants.VacationStatus)
            {
                cbbStatus.Items.Add(status);
                if (status == "Pending")
                {
                    cbbStatus.SelectedItem = status;
                }
            }
        }

        private void Retrieve()
        {
            var vacationList = Global.Context.EmployeeVacations.Where(o => o.IsDeleted == false);

            if (Global.CurrentUser.RoleID == 2 || Global.CurrentUser.RoleID == 3)
            {
                var offceid = Global.CurrentUser.OfficeDepartment_lnk.FirstOrDefault().OfficeCode;
                vacationList = vacationList.Where(o => o.Employee.OfficeDepartment_lnk.Select(a => a.OfficeCode).FirstOrDefault() == offceid && o.Employee.RoleID == 1);
            }
            else if (Global.CurrentUser.RoleID == 5)
            {
                vacationList = vacationList.Where(o => o.Employee.RoleID == 2 || o.Employee.RoleID == 3);
            }

            if (!string.IsNullOrWhiteSpace(tbEmployeeName.Text))
            {
                vacationList = vacationList.Where(o => o.Employee.EnglishName.Contains(tbEmployeeName.Text) || o.Employee.ChineseName.Contains(tbEmployeeName.Text));
            }
            if (!string.IsNullOrWhiteSpace(tbOperator.Text))
            {
                vacationList = vacationList.Where(o => o.Employee1.ChineseName.Contains(tbOperator.Text) || o.Employee1.EnglishName.Contains(tbOperator.Text));
            }
            if (!string.IsNullOrWhiteSpace(cbbStatus.Text))
            {
                vacationList = vacationList.Where(o => o.Status == cbbStatus.Text);
            }
            if (!string.IsNullOrWhiteSpace(cbbVacationType.Text))
            {
                ComboBoxItem item = cbbVacationType.SelectedItem as ComboBoxItem;
                VacationType_lkp type = item.Value as VacationType_lkp;
                vacationList = vacationList.Where(o => o.VacationType_lkp.VacationTypeCode == type.VacationTypeCode);
            }

            lvwData.Items.Clear();
            lvwData.BeginUpdate();
            int i = 1;
            foreach (var vacation in vacationList)
            {
                ListViewItem lvwItem = new ListViewItem("");
                lvwItem.SubItems.AddRange(new string[10]);

                lvwItem.SubItems[0].Text = i.ToString();
                lvwItem.SubItems[1].Text = vacation.Employee.EnglishName + "(" + vacation.Employee.ChineseName + ")";
                lvwItem.SubItems[2].Text = LanLayer.GetTrans(vacation.VacationType_lkp.Content_id.Value);
                lvwItem.SubItems[3].Text = vacation.FromTime.HasValue? vacation.FromTime.Value.ToString("yyyy-MM-dd hh:mm:ss") : "";
                lvwItem.SubItems[4].Text = vacation.EndTime.HasValue ? vacation.EndTime.Value.ToString("yyyy-MM-dd hh:mm:ss") : "";
                lvwItem.SubItems[5].Text = vacation.Length.HasValue ? Utility.ConvertLengthToString(vacation.Length.Value) : "";
                lvwItem.SubItems[6].Text = vacation.Status;
                lvwItem.SubItems[7].Text = vacation.Employee1 == null ? "" : vacation.Employee1.EnglishName + "(" + vacation.Employee1.ChineseName + ")";
                lvwItem.SubItems[8].Text = vacation.OperateTime.HasValue ? vacation.OperateTime.Value.ToString("yyyy-MM-dd hh:mm:ss") : "";
                

                decimal finalHours = 0;
                var vacationbase = Global.Context.VacationBases.Where(o => o.Employee.EmployeeID == vacation.EmployeeID && o.IsDeleted == false && o.IsCurrent && o.VacationTypeCode == "ANN").FirstOrDefault();
                if (vacationbase != null)
                {
                    finalHours = vacationbase.FinalHours.HasValue ? vacationbase.FinalHours.Value : 0;
                }

                lvwItem.SubItems[9].Text = Utility.ConvertLengthToString(finalHours);

                lvwItem.Tag = vacation;

                lvwData.Items.Add(lvwItem);
                i++;
            }
        }

        private void lvwData_DoubleClick(object sender, EventArgs e)
        {
            if (lvwData.SelectedItem != null)
            {
                var currItem = lvwData.SelectedItem.Tag as EmployeeVacation;
                ApproveLeaveForm form = new ApproveLeaveForm(currItem, true);
                form.ShowDialog();
                form.FormClosed += new Gizmox.WebGUI.Forms.Form.FormClosedEventHandler(form_FormClosed);
            }
        }

        void form_FormClosed(object sender, FormClosedEventArgs e)
        {
            Retrieve();
        }

        private void queryControl1_FilterQueryClick(object sender, EventArgs e)
        {
            Retrieve();
        }
    }
}